package org.hang.gift.strategy;


import org.apache.commons.lang.StringUtils;
import org.hang.gift.strategy.impl.ShopCarDataSyncKeyGroupByHashStrategy;

public class ShopCarDataSyncKeyGroupStrategyFactory {


    /**
     * 专门用来获取策略的工厂
     * @return
     */
    public static ShopCarDataSyncKeyGroupStrategy getStrategy(String clazz){
        try {
            // 如果clazz为空,要不然就报错,要不然就返回一个默认对象
            if(StringUtils.isBlank(clazz)){
                return new ShopCarDataSyncKeyGroupByHashStrategy();
            }
            Class<?> aClass = Class.forName(clazz);
            Object instance = aClass.newInstance();
            // 如果当前实例对象是ShopCarDataSyncKeyGroupStrategy的子类
            if(instance instanceof ShopCarDataSyncKeyGroupStrategy){
                return (ShopCarDataSyncKeyGroupStrategy)instance;
            }
            // 如果当前传入的clazz不属于当前ShopCarDataSyncKeyGroupStrategy的子类,就进行报错
            throw new RuntimeException("not support this strategy");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

}
